<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Maven快速入门 | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="项目管理">
    <meta name="description" content="Maven介绍什么是maven：Maven项目对象模型(POM)，可以通过一小段描述信息来管理项目的构建，报告和文档的软件项目管理工具。 Maven是跨平台的项目管理工具。它是使用java开发的，要依赖与jdk1.6及以上。 maven主要有两大功能：项目构建，依赖(指的就是jar包)管理。 什么是构建：简单来说就是软件项目生产的整个过程，比如这个过程应该包括：  文档和代码的生成（有些项目会使用">
<meta name="keywords" content="项目管理">
<meta property="og:type" content="article">
<meta property="og:title" content="Maven快速入门">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="Maven介绍什么是maven：Maven项目对象模型(POM)，可以通过一小段描述信息来管理项目的构建，报告和文档的软件项目管理工具。 Maven是跨平台的项目管理工具。它是使用java开发的，要依赖与jdk1.6及以上。 maven主要有两大功能：项目构建，依赖(指的就是jar包)管理。 什么是构建：简单来说就是软件项目生产的整个过程，比如这个过程应该包括：  文档和代码的生成（有些项目会使用">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/%E6%9E%84%E5%BB%BA%E8%BF%87%E7%A8%8B.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E6%A6%82%E5%BF%B5%E6%A8%A1%E5%9E%8B.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E7%9B%AE%E5%BD%95.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/mvn%20compile.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E6%B5%8B%E8%AF%95.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/%E8%AE%BE%E7%BD%AE%E8%87%AA%E5%B7%B1%E7%9A%84Maven%E8%B7%AF%E5%BE%84.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/%E8%AE%BE%E7%BD%AEMaven%E7%94%A8%E6%88%B7%E9%85%8D%E7%BD%AE.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/%E4%B8%8D%E9%80%9A%E8%BF%87%E9%AA%A8%E6%9E%B6%E5%88%9B%E5%BB%BAMaven%E9%A1%B9%E7%9B%AE.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E4%BE%9D%E8%B5%96%E4%BC%A0%E9%80%92.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E4%BE%9D%E8%B5%96%E8%8C%83%E5%9B%B4%E5%AF%B9%E4%BC%A0%E9%80%92%E4%BE%9D%E8%B5%96%E5%BD%B1%E5%93%8D.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/MavenWeb%E5%BA%94%E7%94%A81.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E7%88%B6%E5%B7%A5%E7%A8%8B.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E5%AD%90%E5%B7%A5%E7%A8%8B.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E8%81%9A%E5%90%88%E5%B7%A5%E7%A8%8B.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E8%81%9A%E5%90%88Dao.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/maven_service.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven_controller.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Maven%E8%81%9A%E5%90%88%E6%88%90%E5%8A%9F.png">
<meta property="og:updated_time" content="2017-12-05T09:57:35.573Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Maven快速入门">
<meta name="twitter:description" content="Maven介绍什么是maven：Maven项目对象模型(POM)，可以通过一小段描述信息来管理项目的构建，报告和文档的软件项目管理工具。 Maven是跨平台的项目管理工具。它是使用java开发的，要依赖与jdk1.6及以上。 maven主要有两大功能：项目构建，依赖(指的就是jar包)管理。 什么是构建：简单来说就是软件项目生产的整个过程，比如这个过程应该包括：  文档和代码的生成（有些项目会使用">
<meta name="twitter:image" content="http://om6odecqr.bkt.clouddn.com/%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA.png">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Maven快速入门</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Maven快速入门</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-08T14:23:00.000Z" itemprop="datePublished" class="page-time">
  2017-08-08
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Maven介绍"><span class="post-toc-number">1.</span> <span class="post-toc-text">Maven介绍</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是maven："><span class="post-toc-number">1.1.</span> <span class="post-toc-text">什么是maven：</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是构建："><span class="post-toc-number">1.2.</span> <span class="post-toc-text">什么是构建：</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#项目构建的方式："><span class="post-toc-number">1.3.</span> <span class="post-toc-text">项目构建的方式：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-Eclipse"><span class="post-toc-number">1.3.1.</span> <span class="post-toc-text">1 Eclipse</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-Ant"><span class="post-toc-number">1.3.2.</span> <span class="post-toc-text">2 Ant</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-Maven"><span class="post-toc-number">1.3.3.</span> <span class="post-toc-text">3 Maven</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven模型："><span class="post-toc-number">1.4.</span> <span class="post-toc-text">Maven模型：</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Maven安装配置-Ubuntu"><span class="post-toc-number">2.</span> <span class="post-toc-text">Maven安装配置(Ubuntu)</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven安装："><span class="post-toc-number">2.1.</span> <span class="post-toc-text">Maven安装：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-Maven的下载"><span class="post-toc-number">2.1.1.</span> <span class="post-toc-text">1 Maven的下载</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-解压到自定义的目录"><span class="post-toc-number">2.1.2.</span> <span class="post-toc-text">2 解压到自定义的目录</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-编辑环境变量"><span class="post-toc-number">2.1.3.</span> <span class="post-toc-text">3 编辑环境变量</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-添加环境变量"><span class="post-toc-number">2.1.4.</span> <span class="post-toc-text">4 添加环境变量</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#5-使之生效"><span class="post-toc-number">2.1.5.</span> <span class="post-toc-text">5 使之生效</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#6-查看是否配置成功"><span class="post-toc-number">2.1.6.</span> <span class="post-toc-text">6 查看是否配置成功</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven的配置："><span class="post-toc-number">2.2.</span> <span class="post-toc-text">Maven的配置：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-全局配置-默认"><span class="post-toc-number">2.2.1.</span> <span class="post-toc-text">1 全局配置(默认)</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-用户配置"><span class="post-toc-number">2.2.2.</span> <span class="post-toc-text">2 用户配置</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#创建Maven工程"><span class="post-toc-number">3.</span> <span class="post-toc-text">创建Maven工程</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven的工程结构："><span class="post-toc-number">3.1.</span> <span class="post-toc-text">Maven的工程结构：</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven的工程创建："><span class="post-toc-number">3.2.</span> <span class="post-toc-text">Maven的工程创建：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-根据Maven的目录结构创建helloMaven工程"><span class="post-toc-number">3.2.1.</span> <span class="post-toc-text">1 根据Maven的目录结构创建helloMaven工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-创建HelloWorld-java"><span class="post-toc-number">3.2.2.</span> <span class="post-toc-text">2 创建HelloWorld.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-创建TestHelloWorld-java"><span class="post-toc-number">3.2.3.</span> <span class="post-toc-text">3 创建TestHelloWorld.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-配置pom-xml"><span class="post-toc-number">3.2.4.</span> <span class="post-toc-text">4 配置pom.xml</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven命令的使用："><span class="post-toc-number">3.3.</span> <span class="post-toc-text">Maven命令的使用：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-mvn-compile"><span class="post-toc-number">3.3.1.</span> <span class="post-toc-text">1 mvn compile</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-mvn-clean"><span class="post-toc-number">3.3.2.</span> <span class="post-toc-text">2 mvn clean</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-mvn-test"><span class="post-toc-number">3.3.3.</span> <span class="post-toc-text">3 mvn test</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-mvn-package"><span class="post-toc-number">3.3.4.</span> <span class="post-toc-text">4 mvn package</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#5-mvn-install"><span class="post-toc-number">3.3.5.</span> <span class="post-toc-text">5 mvn install</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#6-组合命令"><span class="post-toc-number">3.3.6.</span> <span class="post-toc-text">6 组合命令</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-1-mvn-clean-compile"><span class="post-toc-number">3.3.6.1.</span> <span class="post-toc-text">6.1 mvn clean compile</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-2-mvn-clean-test"><span class="post-toc-number">3.3.6.2.</span> <span class="post-toc-text">6.2 mvn clean test</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-3-mvn-clean-package"><span class="post-toc-number">3.3.6.3.</span> <span class="post-toc-text">6.3 mvn clean package</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-4-mvn-clean-install"><span class="post-toc-number">3.3.6.4.</span> <span class="post-toc-text">6.4 mvn clean install</span></a></li></ol></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#M2Eclipse"><span class="post-toc-number">4.</span> <span class="post-toc-text">M2Eclipse</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#安装配置M2Eclipse插件："><span class="post-toc-number">4.1.</span> <span class="post-toc-text">安装配置M2Eclipse插件：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-下载与安装插件"><span class="post-toc-number">4.1.1.</span> <span class="post-toc-text">1 下载与安装插件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-设置你的Maven安装目录"><span class="post-toc-number">4.1.2.</span> <span class="post-toc-text">2 设置你的Maven安装目录</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-设置用户配置文件"><span class="post-toc-number">4.1.3.</span> <span class="post-toc-text">3 设置用户配置文件</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#创建Maven工程-1"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">创建Maven工程</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-通过骨架创建maven工程"><span class="post-toc-number">4.2.1.</span> <span class="post-toc-text">1 通过骨架创建maven工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-创建MavenFirst-java"><span class="post-toc-number">4.2.2.</span> <span class="post-toc-text">2 创建MavenFirst.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-创建TestMavenFirst-java"><span class="post-toc-number">4.2.3.</span> <span class="post-toc-text">3 创建TestMavenFirst.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-执行maven命令进行测试"><span class="post-toc-number">4.2.4.</span> <span class="post-toc-text">4 执行maven命令进行测试</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#2-不通过骨架创建Maven工程："><span class="post-toc-number">4.3.</span> <span class="post-toc-text">2 不通过骨架创建Maven工程：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-不通过骨架创建maven工程"><span class="post-toc-number">4.3.1.</span> <span class="post-toc-text">1 不通过骨架创建maven工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-修改pom文件"><span class="post-toc-number">4.3.2.</span> <span class="post-toc-text">2 修改pom文件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#创建MavenSecond-java"><span class="post-toc-number">4.3.3.</span> <span class="post-toc-text">创建MavenSecond.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#创建TestMavenSecond-java"><span class="post-toc-number">4.3.4.</span> <span class="post-toc-text">创建TestMavenSecond.java</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Maven核心概念"><span class="post-toc-number">5.</span> <span class="post-toc-text">Maven核心概念</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#坐标："><span class="post-toc-number">5.1.</span> <span class="post-toc-text">坐标：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-弄清什么是坐标"><span class="post-toc-number">5.1.1.</span> <span class="post-toc-text">1 弄清什么是坐标</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-Maven坐标的主要组成"><span class="post-toc-number">5.1.2.</span> <span class="post-toc-text">2 Maven坐标的主要组成</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#依赖管理："><span class="post-toc-number">5.2.</span> <span class="post-toc-text">依赖管理：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-依赖范围"><span class="post-toc-number">5.2.1.</span> <span class="post-toc-text">1 依赖范围</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-依赖传递"><span class="post-toc-number">5.2.2.</span> <span class="post-toc-text">2 依赖传递</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-2-直接依赖和间接依赖"><span class="post-toc-number">5.2.2.1.</span> <span class="post-toc-text">2.2 直接依赖和间接依赖</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-3-依赖范围对传递依赖的影响"><span class="post-toc-number">5.2.2.2.</span> <span class="post-toc-text">2.3 依赖范围对传递依赖的影响</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-依赖冲突"><span class="post-toc-number">5.2.3.</span> <span class="post-toc-text">3 依赖冲突</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-跨pom文件的冲突"><span class="post-toc-number">5.2.3.1.</span> <span class="post-toc-text">3.1 跨pom文件的冲突</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-2-同一个pom文件的冲突"><span class="post-toc-number">5.2.3.2.</span> <span class="post-toc-text">3.2 同一个pom文件的冲突</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-可选依赖"><span class="post-toc-number">5.2.4.</span> <span class="post-toc-text">4 可选依赖</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#5-排除依赖"><span class="post-toc-number">5.2.5.</span> <span class="post-toc-text">5 排除依赖</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#生命周期"><span class="post-toc-number">5.3.</span> <span class="post-toc-text">生命周期</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-clean：清理项目"><span class="post-toc-number">5.3.1.</span> <span class="post-toc-text">1 clean：清理项目</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-default：构建项目"><span class="post-toc-number">5.3.2.</span> <span class="post-toc-text">2 default：构建项目</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-site：生成项目站点"><span class="post-toc-number">5.3.3.</span> <span class="post-toc-text">3 site：生成项目站点</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Maven插件"><span class="post-toc-number">5.4.</span> <span class="post-toc-text">Maven插件</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-Maven编译插件"><span class="post-toc-number">5.4.1.</span> <span class="post-toc-text">1 Maven编译插件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-安装Tomcat插件"><span class="post-toc-number">5.4.2.</span> <span class="post-toc-text">2 安装Tomcat插件</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#继承"><span class="post-toc-number">5.5.</span> <span class="post-toc-text">继承</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-创建父工程"><span class="post-toc-number">5.5.1.</span> <span class="post-toc-text">1 创建父工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-创建子工程"><span class="post-toc-number">5.5.2.</span> <span class="post-toc-text">2 创建子工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-父工程统一依赖jar包"><span class="post-toc-number">5.5.3.</span> <span class="post-toc-text">3 父工程统一依赖jar包</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-父工程统一管理版本号"><span class="post-toc-number">5.5.4.</span> <span class="post-toc-text">4 父工程统一管理版本号</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#5-父工程中版本号提取"><span class="post-toc-number">5.5.5.</span> <span class="post-toc-text">5 父工程中版本号提取</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#聚合"><span class="post-toc-number">5.6.</span> <span class="post-toc-text">聚合</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-创建聚合工程"><span class="post-toc-number">5.6.1.</span> <span class="post-toc-text">1 创建聚合工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-创建持久层工程"><span class="post-toc-number">5.6.2.</span> <span class="post-toc-text">2 创建持久层工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-创建业务层工程"><span class="post-toc-number">5.6.3.</span> <span class="post-toc-text">3 创建业务层工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-创建表现层工程"><span class="post-toc-number">5.6.4.</span> <span class="post-toc-text">4 创建表现层工程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#5-运行maven-web聚合工程"><span class="post-toc-number">5.6.5.</span> <span class="post-toc-text">5 运行maven-web聚合工程</span></a></li></ol></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Maven_primary_01"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Maven快速入门</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年08月08日 22:23" datetime="2017-08-08T14:23:00.000Z"  itemprop="datePublished">2017-08-08</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="Maven介绍"><a href="#Maven介绍" class="headerlink" title="Maven介绍"></a>Maven介绍</h2><h3 id="什么是maven："><a href="#什么是maven：" class="headerlink" title="什么是maven："></a>什么是maven：</h3><p>Maven项目对象模型(POM)，可以通过一小段描述信息来管理项目的构建，报告和文档的<strong>软件项目管理工具</strong>。</p>
<p>Maven是跨平台的项目管理工具。它是使用java开发的，要依赖与jdk1.6及以上。</p>
<p>maven主要有两大功能：<strong>项目构建</strong>，<strong>依赖(指的就是jar包)管理</strong>。</p>
<h3 id="什么是构建："><a href="#什么是构建：" class="headerlink" title="什么是构建："></a>什么是构建：</h3><p>简单来说就是软件项目生产的整个过程，比如这个过程应该包括：</p>
<ul>
<li>文档和代码的生成（有些项目会使用代码生成工具，比如hibernate生成ORM映射的逆向工程）</li>
<li>代码的编译，测试和打包</li>
<li>打包好的代码进行分发或者部署</li>
</ul>
<p><img src="http://om6odecqr.bkt.clouddn.com/%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA.png" alt="项目构建"></p>
<p>有了Maven构建中的这些过程都能够进行良好的定义，而且Maven能够帮我们串起来形成一个自动构建过程，这比我们手动执行要高效的多：<br><img src="http://om6odecqr.bkt.clouddn.com/%E6%9E%84%E5%BB%BA%E8%BF%87%E7%A8%8B.png" alt="构建过程"></p>
<h3 id="项目构建的方式："><a href="#项目构建的方式：" class="headerlink" title="项目构建的方式："></a>项目构建的方式：</h3><h4 id="1-Eclipse"><a href="#1-Eclipse" class="headerlink" title="1 Eclipse"></a>1 Eclipse</h4><p>手工操作较多，项目的构建过程都是独立的，很难一步完成。比如：编译、测试、部署等，开发时每个人的IDE配置都不同，很容易出现本地代码换地方编译就出错。</p>
<h4 id="2-Ant"><a href="#2-Ant" class="headerlink" title="2 Ant"></a>2 Ant</h4><p>Ant只是一个项目构建工具，它没有集成依赖管理。Ant在进行项目构建时，它没有对项目目录结果进行约定，需要手动指定源文件，类文件等目录地址。同时它执行task时，需要显示指定依赖的task，这样会造成大量的代码重复。</p>
<h4 id="3-Maven"><a href="#3-Maven" class="headerlink" title="3 Maven"></a>3 Maven</h4><p>Maven不仅是一个项目构建工具，更是一个项目管理工具。它在项目构建工程中，比Ant更全面，更灵活。</p>
<p>Maven在对项目构建时，它对项目目录结构拥有约定，知道你的源代码在哪里，类文件应该放到那里去。</p>
<p>它拥有生命周期的概念，Maven的生命周期时有顺序的，在执行后面的生命周期的任务时，不需要显式的配置前面任务的生命周期。例如执行mvn install就可以自动执行编译，测试，打包等构建过程。</p>
<h3 id="Maven模型："><a href="#Maven模型：" class="headerlink" title="Maven模型："></a>Maven模型：</h3><p><img src="http://om6odecqr.bkt.clouddn.com/Maven%E6%A6%82%E5%BF%B5%E6%A8%A1%E5%9E%8B.png" alt="Maven概念模型"></p>
<hr>
<h2 id="Maven安装配置-Ubuntu"><a href="#Maven安装配置-Ubuntu" class="headerlink" title="Maven安装配置(Ubuntu)"></a>Maven安装配置(Ubuntu)</h2><h3 id="Maven安装："><a href="#Maven安装：" class="headerlink" title="Maven安装："></a>Maven安装：</h3><h4 id="1-Maven的下载"><a href="#1-Maven的下载" class="headerlink" title="1 Maven的下载"></a>1 Maven的下载</h4><p><a href="http://maven.apache.org/index.html" target="_blank" rel="external">Maven官网</a>，本人下载的maven版本为3.5.0，需要依赖jdk1.7及以上。</p>
<h4 id="2-解压到自定义的目录"><a href="#2-解压到自定义的目录" class="headerlink" title="2 解压到自定义的目录"></a>2 解压到自定义的目录</h4><p><img src="http://om6odecqr.bkt.clouddn.com/Maven%E7%9B%AE%E5%BD%95.png" alt="Maven目录"></p>
<h4 id="3-编辑环境变量"><a href="#3-编辑环境变量" class="headerlink" title="3 编辑环境变量"></a>3 编辑环境变量</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo gedit /etc/profile</div></pre></td></tr></table></figure>
<h4 id="4-添加环境变量"><a href="#4-添加环境变量" class="headerlink" title="4 添加环境变量"></a>4 添加环境变量</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#</span> MAVEN_HOME</div><div class="line">export MAVEN_HOME=/root/IDE/maven-3.5</div><div class="line">export MAVEN_OPTS="-Xms256m -Xmx512m"</div><div class="line">export PATH=$&#123;MAVEN_HOME&#125;/bin:$PATH</div></pre></td></tr></table></figure>
<h4 id="5-使之生效"><a href="#5-使之生效" class="headerlink" title="5 使之生效"></a>5 使之生效</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">. /etc/profile</div></pre></td></tr></table></figure>
<h4 id="6-查看是否配置成功"><a href="#6-查看是否配置成功" class="headerlink" title="6 查看是否配置成功"></a>6 查看是否配置成功</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">root@sqdyy-X555LI:~# mvn -version</div><div class="line">Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)</div><div class="line">Maven home: /root/IDE/maven-3.5</div><div class="line">Java version: 1.8.0_40, vendor: Oracle Corporation</div><div class="line">Java home: /usr/lib/jvm/jdk1.8.0_40/jre</div><div class="line">Default locale: zh_CN, platform encoding: UTF-8</div><div class="line">OS name: "linux", version: "4.10.0-30-generic", arch: "amd64", family: "unix"</div></pre></td></tr></table></figure>
<h3 id="Maven的配置："><a href="#Maven的配置：" class="headerlink" title="Maven的配置："></a>Maven的配置：</h3><p>Maven有两个settings.xml配置文件，一个是全局配置文件，一个是用户配置文件。</p>
<h4 id="1-全局配置-默认"><a href="#1-全局配置-默认" class="headerlink" title="1 全局配置(默认)"></a>1 全局配置(默认)</h4><p>/root/IDE/maven-3.5/conf/settings.xml 是Maven全局的配置文件。<br>该文件中配置了maven本地仓库的地址<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- localRepository</span></div><div class="line"><span class="comment">  | The path to the local repository maven will use to store artifacts.</span></div><div class="line"><span class="comment">  |</span></div><div class="line"><span class="comment">  | Default: $&#123;user.home&#125;/.m2/repository</span></div><div class="line"><span class="comment"> &lt;localRepository&gt;/path/to/local/repo&lt;/localRepository&gt;</span></div><div class="line"><span class="comment"> --&gt;</span></div></pre></td></tr></table></figure></p>
<p>其中${user.home}表示当前用户路径/root/.m2/repository，该目录时本地仓库的目录。</p>
<h4 id="2-用户配置"><a href="#2-用户配置" class="headerlink" title="2 用户配置"></a>2 用户配置</h4><p>目录/root/.m2/settings.xml是用户的配置文件（默认没有该文件，需要将全局配置文件拷贝过来再进行修改），</p>
<p><strong>注意：</strong> 一般本地仓库的地址不使用默认配置，通常情况下需要在用户配置中，配置新的仓库地址，配置步骤如下：</p>
<ul>
<li>1 创建一个本地仓库目录，比如：/root/文档/MyCode/Maven/repository</li>
<li>2 复制maven的全局配置文件到/root/.m2目录下，即创建用户配置文件</li>
<li><p>3 修改maven的用户配置文件：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- localRepository</span></div><div class="line"><span class="comment"> | The path to the local repository maven will use to store artifacts.</span></div><div class="line"><span class="comment"> |</span></div><div class="line"><span class="comment"> | Default: $&#123;user.home&#125;/.m2/repository</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">--&gt;</span>  </div><div class="line"><span class="comment">&lt;!-- 将默认的地址改为指定的本地仓库地址 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">localRepository</span>&gt;</span>/root/文档/MyCode/Maven/repository<span class="tag">&lt;/<span class="name">localRepository</span>&gt;</span></div></pre></td></tr></table></figure>
</li>
<li><p>4 把Maven中央仓库换成阿里云，用于提高构建速度<strong>(不弄你会哭)</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 在mirrors中配置mirror --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>alimaven<span class="tag">&lt;/<span class="name">id</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span>        </div><div class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>aliyun maven<span class="tag">&lt;/<span class="name">name</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span></div><div class="line">        http://maven.aliyun.com/nexus/content/groups/public/</div><div class="line">    <span class="tag">&lt;/<span class="name">url</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span></div></pre></td></tr></table></figure>
</li>
</ul>
<p><strong>注意：</strong>用户级别的仓库在全局配置中一旦设置，全局配置将不再生效，转用用户所设置的仓库.</p>
<hr>
<h2 id="创建Maven工程"><a href="#创建Maven工程" class="headerlink" title="创建Maven工程"></a>创建Maven工程</h2><h3 id="Maven的工程结构："><a href="#Maven的工程结构：" class="headerlink" title="Maven的工程结构："></a>Maven的工程结构：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">helloMaven工程</div><div class="line">    |-- src (源码包)</div><div class="line">        |-- main (正常的源码包)</div><div class="line">            |-- java (.java文件的目录)</div><div class="line">            |-- resources (资源文件的目录)</div><div class="line">        |-- test (测试的源码包)</div><div class="line">            |-- java (.java文件的目录)</div><div class="line">            |-- resources (资源文件的目录)</div><div class="line">    |-- target (class文件、报告等信息存储的地方)</div><div class="line">    |-- pom.xml (maven工程的描述文件)</div></pre></td></tr></table></figure>
<h3 id="Maven的工程创建："><a href="#Maven的工程创建：" class="headerlink" title="Maven的工程创建："></a>Maven的工程创建：</h3><h4 id="1-根据Maven的目录结构创建helloMaven工程"><a href="#1-根据Maven的目录结构创建helloMaven工程" class="headerlink" title="1 根据Maven的目录结构创建helloMaven工程"></a>1 根据Maven的目录结构创建helloMaven工程</h4><h4 id="2-创建HelloWorld-java"><a href="#2-创建HelloWorld-java" class="headerlink" title="2 创建HelloWorld.java"></a>2 创建HelloWorld.java</h4><p>在/helloMaven/src/main/java/cn/sqdyy/maven目录下新建HelloMaven.java<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.sqdyy.maven;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloMaven</span></span>&#123;</div><div class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String name)</span></span>&#123;</div><div class="line">		<span class="keyword">return</span> <span class="string">"Hello "</span> + name;	</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h4 id="3-创建TestHelloWorld-java"><a href="#3-创建TestHelloWorld-java" class="headerlink" title="3 创建TestHelloWorld.java"></a>3 创建TestHelloWorld.java</h4><p>在/helloMaven/src/test/java/cn/sqdyy/maven目录下新建TestHelloMaven.java<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.sqdyy.maven;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"><span class="keyword">import</span> <span class="keyword">static</span> junit.framework.Assert.*;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestHelloMaven</span></span>&#123;</div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSayHello</span><span class="params">()</span></span>&#123;</div><div class="line">		HelloMaven hm = <span class="keyword">new</span> HelloMaven();</div><div class="line">		String result = hm.sayHello(<span class="string">"maven"</span>); <span class="comment">// Hello maven</span></div><div class="line">		<span class="comment">//junit 测试：断言</span></div><div class="line">		assertEquals(<span class="string">"Hello maven1"</span>, result);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h4 id="4-配置pom-xml"><a href="#4-配置pom-xml" class="headerlink" title="4 配置pom.xml"></a>4 配置pom.xml</h4><p>暂时可作为了解，后面会进行详细描述：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 版本：4.0.0 --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 组织名称：暂时使用 组织名称+项目名称 作为组织名称 --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 组织名称：实际名称 按照访问路径规范设置，通常以功能作为名称：eg: junit spring --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy.maven<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 项目名称 --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>HelloMaven<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 当前项目版本号：同一个项目开发过程中可以发布多个版本，此处标示0.0.1版 --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 当前项目版本号：每个工程发布后可以发布多个版本，依赖时调取不同的版本，使用不同的版本号 --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 名称：可省略 --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">name</span>&gt;</span>HelloMaven<span class="tag">&lt;/<span class="name">name</span>&gt;</span></div><div class="line">  </div><div class="line">  <span class="comment">&lt;!-- 依赖关系 --&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">		<span class="comment">&lt;!-- 依赖设置 --&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">			<span class="comment">&lt;!-- 依赖组织名称 --&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">			<span class="comment">&lt;!-- 依赖项目名称 --&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">			<span class="comment">&lt;!-- 依赖版本名称 --&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">version</span>&gt;</span>4.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">			<span class="comment">&lt;!-- 依赖范围：test包下依赖该设置 --&gt;</span></div><div class="line">			<span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></div><div class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span>		</div><div class="line">		</div><div class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>到此Maven工程创建成功。</p>
<h3 id="Maven命令的使用："><a href="#Maven命令的使用：" class="headerlink" title="Maven命令的使用："></a>Maven命令的使用：</h3><p>Maven的命令要在pom.xml所在的目录下执行。</p>
<h4 id="1-mvn-compile"><a href="#1-mvn-compile" class="headerlink" title="1 mvn compile"></a>1 mvn compile</h4><p>执行mvn compile命令，完成编译工作，会生成target目录，该目录中存放了编译后的字节码文件。<br><img src="http://om6odecqr.bkt.clouddn.com/mvn%20compile.png" alt="mvn compile"></p>
<h4 id="2-mvn-clean"><a href="#2-mvn-clean" class="headerlink" title="2 mvn clean"></a>2 mvn clean</h4><p>执行mvn clean命令，会将target目录删除。</p>
<h4 id="3-mvn-test"><a href="#3-mvn-test" class="headerlink" title="3 mvn test"></a>3 mvn test</h4><p>执行mvn test命令，完成单元测试操作，会在target目录中生成三个文件夹：surefire、surefire-reports（测试报告）、test-classes（测试的字节码文件），文中前面部分留下了一处陷阱，可以通过控制台或测试报告看到错误信息，自行修正即可：<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E6%B5%8B%E8%AF%95.png" alt="maven测试"></p>
<h4 id="4-mvn-package"><a href="#4-mvn-package" class="headerlink" title="4 mvn package"></a>4 mvn package</h4><p>执行mvn package命令，完成打包操作，会在target目录中生成一个文件，该文件可能是jar、war。</p>
<h4 id="5-mvn-install"><a href="#5-mvn-install" class="headerlink" title="5 mvn install"></a>5 mvn install</h4><p>执行mvn install命令，完成将打好的jar包安装到本地仓库的操作，会在本地仓库中出现安装后的jar包，方便其他工程引用。</p>
<h4 id="6-组合命令"><a href="#6-组合命令" class="headerlink" title="6 组合命令"></a>6 组合命令</h4><h5 id="6-1-mvn-clean-compile"><a href="#6-1-mvn-clean-compile" class="headerlink" title="6.1 mvn clean compile"></a>6.1 mvn clean compile</h5><p>组合指令，先执行clean，再执行compile，通常应用于上线前执行，清除测试类。</p>
<h5 id="6-2-mvn-clean-test"><a href="#6-2-mvn-clean-test" class="headerlink" title="6.2 mvn clean test"></a>6.2 mvn clean test</h5><p>组合指令，先执行clean，再执行test，通常应用于测试环节。</p>
<h5 id="6-3-mvn-clean-package"><a href="#6-3-mvn-clean-package" class="headerlink" title="6.3 mvn clean package"></a>6.3 mvn clean package</h5><p>组合指令，先执行clean，在执行package，将项目打包，通常应用于发布前的执行过程：</p>
<ul>
<li>清理 —&gt; 清空环节</li>
<li>编译 —&gt; 编译源码</li>
<li>测试 —&gt; 测试源码</li>
<li>打包 —&gt; 将编译的非测试类打包</li>
</ul>
<h5 id="6-4-mvn-clean-install"><a href="#6-4-mvn-clean-install" class="headerlink" title="6.4 mvn clean install"></a>6.4 mvn clean install</h5><p>组合指令，先执行clean，再执行install，将项目打包，通常应用于发布前的执行过程：</p>
<ul>
<li>清理 —&gt; 清空环节</li>
<li>编译 —&gt; 编译源码</li>
<li>测试 —&gt; 测试源码</li>
<li>打包 —&gt; 将编译的非测试类打包</li>
<li>部署 —&gt; 将打好的包发布到资源仓库中</li>
</ul>
<hr>
<h2 id="M2Eclipse"><a href="#M2Eclipse" class="headerlink" title="M2Eclipse"></a>M2Eclipse</h2><h3 id="安装配置M2Eclipse插件："><a href="#安装配置M2Eclipse插件：" class="headerlink" title="安装配置M2Eclipse插件："></a>安装配置M2Eclipse插件：</h3><h4 id="1-下载与安装插件"><a href="#1-下载与安装插件" class="headerlink" title="1 下载与安装插件"></a>1 下载与安装插件</h4><p><a href="http://www.eclipse.org/m2e/m2e-downloads.html" target="_blank" rel="external">M2Eclipse</a></p>
<p>由于我使用的是2017版的eclipse，已经帮我集成好了此插件，因此如果你需要自己配置，只需要将下载的文件拷贝到eclipse中的dropins目录中即可完成安装。</p>
<h4 id="2-设置你的Maven安装目录"><a href="#2-设置你的Maven安装目录" class="headerlink" title="2 设置你的Maven安装目录"></a>2 设置你的Maven安装目录</h4><p><img src="http://om6odecqr.bkt.clouddn.com/%E8%AE%BE%E7%BD%AE%E8%87%AA%E5%B7%B1%E7%9A%84Maven%E8%B7%AF%E5%BE%84.png" alt="设置自己的Maven路径"></p>
<h4 id="3-设置用户配置文件"><a href="#3-设置用户配置文件" class="headerlink" title="3 设置用户配置文件"></a>3 设置用户配置文件</h4><p>单独使用命令行方式进行maven的使用时，用户配置文件默认在~/.m2/settings.xml中，如果使用eclipse，则可以任意指定用户配置文件的地址，这里推荐将settings.xml配置到你的Maven本地仓库同级目录下。<br><img src="http://om6odecqr.bkt.clouddn.com/%E8%AE%BE%E7%BD%AEMaven%E7%94%A8%E6%88%B7%E9%85%8D%E7%BD%AE.png" alt="设置用户配置"></p>
<h3 id="创建Maven工程-1"><a href="#创建Maven工程-1" class="headerlink" title="创建Maven工程"></a>创建Maven工程</h3><h4 id="1-通过骨架创建maven工程"><a href="#1-通过骨架创建maven工程" class="headerlink" title="1 通过骨架创建maven工程"></a>1 通过骨架创建maven工程</h4><ul>
<li>步骤1：new→Maven Project，设置项目名和位置，不勾选Create a simple project(勾选则不通过骨架创建)，直接next。</li>
<li>步骤2：选择maven的工作原型(骨架)，这里选择quickstart，然后直接双击或next。</li>
<li>步骤3：要求你输入GroupId(组织名cn.sqdyy)、ArtifactId(项目名maven_first)、Version(版本信息)、Package(工程包名cn.sqdyy.maven_first)信息，输入完成点击finish完成。</li>
</ul>
<h4 id="2-创建MavenFirst-java"><a href="#2-创建MavenFirst-java" class="headerlink" title="2 创建MavenFirst.java"></a>2 创建MavenFirst.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.sqdyy.maven_first;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MavenFirst</span> </span>&#123;</div><div class="line"></div><div class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String name)</span> </span>&#123;</div><div class="line">		<span class="keyword">return</span> <span class="string">"hello "</span> + name;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="3-创建TestMavenFirst-java"><a href="#3-创建TestMavenFirst-java" class="headerlink" title="3 创建TestMavenFirst.java"></a>3 创建TestMavenFirst.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.sqdyy.maven_first;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"></div><div class="line"><span class="keyword">import</span> junit.framework.Assert;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestMavenFirst</span> </span>&#123;</div><div class="line"></div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="comment">// 默认支持的是Junit 4，把pom.xml中的junit版本配置为4.9即可</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSayHello</span><span class="params">()</span> </span>&#123;</div><div class="line">		MavenFirst first = <span class="keyword">new</span> MavenFirst();</div><div class="line">		String result = first.sayHello(<span class="string">"maven"</span>);</div><div class="line">		Assert.assertEquals(<span class="string">"hello maven"</span>, result);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="4-执行maven命令进行测试"><a href="#4-执行maven命令进行测试" class="headerlink" title="4 执行maven命令进行测试"></a>4 执行maven命令进行测试</h4><p>在Eclipse的maven插件中执行maven命令，需要在maven工程或者pom.xml文件上点击右键，选择Run as→maven build..</p>
<p>maven常用命令已经以集成菜单形式出现，但推荐使用maven build和maven build…，注意这两个不是maven的命令。</p>
<p>maven build…提供一个命令输入功能，可以在此功能中输入自定义的maven命令，maven build的功能时执行上次自定义命令，只需要在Goals上输入maven命令即可，并且不需要写mvn命令，例如直接写：clean test。</p>
<h3 id="2-不通过骨架创建Maven工程："><a href="#2-不通过骨架创建Maven工程：" class="headerlink" title="2 不通过骨架创建Maven工程："></a>2 不通过骨架创建Maven工程：</h3><p>通过选择骨架创建maven工程，每次选择骨架时都需要联网下载，如果网络不通畅的情况下会有很长的时间等待。使用不是很方便，所以一般创建工程时可以不选择骨架直接创建工程。</p>
<h4 id="1-不通过骨架创建maven工程"><a href="#1-不通过骨架创建maven工程" class="headerlink" title="1 不通过骨架创建maven工程"></a>1 不通过骨架创建maven工程</h4><ul>
<li>步骤一：new→Maven Project，设置项目名和位置，勾选Create a simple project，直接next。</li>
<li>步骤二：填写以下内容，直接finish。<br><img src="http://om6odecqr.bkt.clouddn.com/%E4%B8%8D%E9%80%9A%E8%BF%87%E9%AA%A8%E6%9E%B6%E5%88%9B%E5%BB%BAMaven%E9%A1%B9%E7%9B%AE.png" alt="不通过骨架创建Maven项目"></li>
</ul>
<h4 id="2-修改pom文件"><a href="#2-修改pom文件" class="headerlink" title="2 修改pom文件"></a>2 修改pom文件</h4><p>这次我们在maven_second工程中依赖使用maven_first工程代码<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 依赖关系 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line"><span class="comment">&lt;!-- 设置junit 4依赖 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"><span class="comment">&lt;!-- 设置在maven_second中依赖maven_first --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven_first<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="创建MavenSecond-java"><a href="#创建MavenSecond-java" class="headerlink" title="创建MavenSecond.java"></a>创建MavenSecond.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.sqdyy.maven_second;</div><div class="line"></div><div class="line"><span class="keyword">import</span> cn.sqdyy.maven_first.MavenFirst;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MavenSecond</span> </span>&#123;</div><div class="line"></div><div class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String name)</span> </span>&#123;</div><div class="line">		MavenFirst first = <span class="keyword">new</span> MavenFirst();</div><div class="line">		<span class="keyword">return</span> first.sayHello(name) + <span class="string">":second"</span>;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="创建TestMavenSecond-java"><a href="#创建TestMavenSecond-java" class="headerlink" title="创建TestMavenSecond.java"></a>创建TestMavenSecond.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.sqdyy.maven_second;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"></div><div class="line"><span class="keyword">import</span> junit.framework.Assert;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestMavenSecond</span> </span>&#123;</div><div class="line"></div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSayHello</span><span class="params">()</span> </span>&#123;</div><div class="line">		MavenSecond second = <span class="keyword">new</span> MavenSecond();</div><div class="line">		String result = second.sayHello(<span class="string">"maven"</span>);</div><div class="line">		Assert.assertEquals(<span class="string">"hello maven:second"</span>, result);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<hr>
<h2 id="Maven核心概念"><a href="#Maven核心概念" class="headerlink" title="Maven核心概念"></a>Maven核心概念</h2><h3 id="坐标："><a href="#坐标：" class="headerlink" title="坐标："></a>坐标：</h3><h4 id="1-弄清什么是坐标"><a href="#1-弄清什么是坐标" class="headerlink" title="1 弄清什么是坐标"></a>1 弄清什么是坐标</h4><p>在平面几何中坐标(x,y)可以标识平面中唯一的一点，在maven中坐标就是为了定位一个唯一确定的jar包，Maven世界拥有大量<strong>构建</strong>，我们需要找到一个用来唯一标识一个构建的统一规范，拥有了统一规范，就可以把查找工作交给机器。</p>
<h4 id="2-Maven坐标的主要组成"><a href="#2-Maven坐标的主要组成" class="headerlink" title="2 Maven坐标的主要组成"></a>2 Maven坐标的主要组成</h4><ul>
<li>groupld：定义当前Maven组织名称</li>
<li>artifactId：定义实际项目名称</li>
<li>version：定义当前项目的当前版本</li>
</ul>
<h3 id="依赖管理："><a href="#依赖管理：" class="headerlink" title="依赖管理："></a>依赖管理：</h3><p>就是对项目中jar包的管理。我们可以在pom文件中定义jar包的GAV坐标，管理依赖，依赖声明主要包含如下元素：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- maven_first --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="1-依赖范围"><a href="#1-依赖范围" class="headerlink" title="1 依赖范围"></a>1 依赖范围</h4><table>
<thead>
<tr>
<th style="text-align:left">依赖范围(scope)</th>
<th style="text-align:left">对主代码classpath有效</th>
<th style="text-align:left">对测试代码classpath有效</th>
<th style="text-align:left">对于运行时classpath有效(被打包)</th>
<th style="text-align:left">例子</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">compile</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">log4j</td>
</tr>
<tr>
<td style="text-align:left">test</td>
<td style="text-align:left">-</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">-</td>
<td style="text-align:left">junit</td>
</tr>
<tr>
<td style="text-align:left">provided</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">-</td>
<td style="text-align:left">servlet-api</td>
</tr>
<tr>
<td style="text-align:left">runtime</td>
<td style="text-align:left">-</td>
<td style="text-align:left">-</td>
<td style="text-align:left">Y</td>
<td style="text-align:left">JDBC Driver implementation</td>
</tr>
</tbody>
</table>
<p>其中依赖关系scope用来控制依赖和编译，测试，运行的classpath的关系，主要的是三种依赖关系如下：</p>
<ul>
<li>1 compile：默认编译依赖范围，对于编译，测试，运行三种classpath都有效</li>
<li>2 test：测试依赖范围，只会测试classpath有效</li>
<li>3 provided：已提供依赖范围，对于编译，测试的classpath都有效，但对于运行无效，例如servlet-api中一般web容器都已经提供，不需要将其一起打包，以防止依赖冲突。</li>
<li>4 runtime：运行时提供，例如：jdbc驱动</li>
</ul>
<h4 id="2-依赖传递"><a href="#2-依赖传递" class="headerlink" title="2 依赖传递"></a>2 依赖传递</h4><h5 id="2-2-直接依赖和间接依赖"><a href="#2-2-直接依赖和间接依赖" class="headerlink" title="2.2 直接依赖和间接依赖"></a>2.2 直接依赖和间接依赖</h5><p>如下图所示，third中使用second，second中使用first。我们说third对于second是第一直接依赖，second对于first是第二直接依赖，third对于first是传递性依赖。第一直接依赖的范围和第二直接依赖的范围决定了传递性依赖的范围。<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E4%BE%9D%E8%B5%96%E4%BC%A0%E9%80%92.png" alt="Maven依赖传递"></p>
<h5 id="2-3-依赖范围对传递依赖的影响"><a href="#2-3-依赖范围对传递依赖的影响" class="headerlink" title="2.3 依赖范围对传递依赖的影响"></a>2.3 依赖范围对传递依赖的影响</h5><p>如下图所示，最左边一列表示第一直接依赖的范围，最上面一行表示第二直接依赖范围，中间交叉单元格表示传递性依赖范围。<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E4%BE%9D%E8%B5%96%E8%8C%83%E5%9B%B4%E5%AF%B9%E4%BC%A0%E9%80%92%E4%BE%9D%E8%B5%96%E5%BD%B1%E5%93%8D.png" alt="Maven依赖传递"><br>测试结论：</p>
<ul>
<li>当第二依赖的范围时compile的时候，传递性依赖的范围与第一直接依赖的范围一致。</li>
<li>当第二直接依赖的范围时test的时候，依赖不会得以传递。</li>
<li>当第二直接依赖的范围时provided的时候，依赖不会传递。</li>
<li>当第二直接依赖的范围时runtime的时候，传递性依赖的范围与第一直接依赖的范围一致，但compile例外，此时传递的依赖范围为runtime。</li>
</ul>
<h4 id="3-依赖冲突"><a href="#3-依赖冲突" class="headerlink" title="3 依赖冲突"></a>3 依赖冲突</h4><p>在maven中存在两种冲突方式：一种是跨pom文件的冲突，一种是同一个pom文件中的冲突</p>
<h5 id="3-1-跨pom文件的冲突"><a href="#3-1-跨pom文件的冲突" class="headerlink" title="3.1 跨pom文件的冲突"></a>3.1 跨pom文件的冲突</h5><p>maven_first的pom文件中依赖junit的4.9版本，于是maven_second和maven_third中也都是使用了4.9版本。</p>
<p>如果maven_second中重新依赖junit的4.8版本，那么maven_second和maven_third中都时使用了4.8版本，这体现了依赖的<strong>就近使用原则</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- maven_second的配置 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h5 id="3-2-同一个pom文件的冲突"><a href="#3-2-同一个pom文件的冲突" class="headerlink" title="3.2 同一个pom文件的冲突"></a>3.2 同一个pom文件的冲突</h5><p>实际上这个问题同样是采用<strong>就近使用原则</strong>，越下面的越近。那么maven_second和maven_third中都时使用了4.6版本。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- maven_second的配置 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="4-可选依赖"><a href="#4-可选依赖" class="headerlink" title="4 可选依赖"></a>4 可选依赖</h4><p>Optional标签标示该依赖可否可选，默认时false。可以理解为，如果为true，则标示该依赖不会传递下去，如果为false，则会传递下去。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- maven_second的配置 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven_first<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 决定此依赖是否向下传递，true不传递，false传递 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="5-排除依赖"><a href="#5-排除依赖" class="headerlink" title="5 排除依赖"></a>5 排除依赖</h4><p>我们假设在本次依赖中maven_first对于maven_third是多余的jar包，这时候我们想把这个没用的jar包排除的话，可以配置Exclusions标签排除依赖，<strong>不需要写版本号</strong>。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- maven_third的配置 --&gt;</span></div><div class="line"><span class="comment">&lt;!--依赖maven_second--maven_first工程--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven_second<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 配置要排除的jar包--&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven_first<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="生命周期"><a href="#生命周期" class="headerlink" title="生命周期"></a>生命周期</h3><p>Maven生命周期就是为了<strong>对所有构建过程进行抽象和统一</strong>。包括<strong>项目清理，初始化，编译，打包，测试，部署</strong>等几乎所有构建步骤。生命周期可以理解为构建工程的步骤。</p>
<p>在Maven中有<strong>三套相互独立</strong>的生命周期，分别是：</p>
<ul>
<li>Clean Lifecycle：在进行真正的构建之前进行一些清理工作。</li>
<li>Default Lifecycle：构建的核心部分，编译，测试，打包，部署等等。</li>
<li>Site Lifecycle：生成项目报告，站点，发布站点。</li>
</ul>
<p>需要注意的是他们是相互独立的，也就是说你用clean就仅仅是清理工作目录，用site就仅仅是生成站点。当然你也可以直接运行mvn clean default site 运行所有这三套生命周期。</p>
<h4 id="1-clean：清理项目"><a href="#1-clean：清理项目" class="headerlink" title="1 clean：清理项目"></a>1 clean：清理项目</h4><p>每套生命周期都由一组阶段(Phase)组成，我们平时在命令行输入命令总会对应于一个特定的阶段，例如mvn clean这个clean是Clean生命周期的一个阶段，它一共包含了三个阶段：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">pre-clean 执行一些需要在clean之前完成的工作</div><div class="line">clean 移除所有上一次构建生成的文件 </div><div class="line">post-clean 执行一些需要在clean之后立刻完成的工作</div></pre></td></tr></table></figure></p>
<p>也就是说，执行mvn clean以上所有阶段都会被运行，它等同于 mvn pre-clean clean post-clean，如果我们运行的是 mvn post-clean，那么pre-clean,clean都会被运行。这是Maven很重要的一个规则，可以大大简化命令行的输入。</p>
<h4 id="2-default：构建项目"><a href="#2-default：构建项目" class="headerlink" title="2 default：构建项目"></a>2 default：构建项目</h4><p>Default生命周期时Maven生命周期中最重要的一个，绝大部分工作都发生在这个生命周期中，这里只截取一些比较重要和常用的阶段：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">validate </div><div class="line">generate-sources </div><div class="line">process-sources </div><div class="line">generate-resources </div><div class="line">process-resources 复制并处理资源文件，至目标目录，准备打包。 </div><div class="line">compile 编译项目的源代码。 </div><div class="line">process-classes </div><div class="line">generate-test-sources </div><div class="line">process-test-sources </div><div class="line">generate-test-resources </div><div class="line">process-test-resources 复制并处理资源文件，至目标测试目录。 </div><div class="line">test-compile 编译测试源代码。 </div><div class="line">process-test-classes </div><div class="line">test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。 </div><div class="line">prepare-package </div><div class="line">package 接受编译好的代码，打包成可发布的格式，如 JAR 。 </div><div class="line">pre-integration-test </div><div class="line">integration-test </div><div class="line">post-integration-test </div><div class="line">verify </div><div class="line">install 将包安装至本地仓库，以让其它项目依赖。 </div><div class="line">deploy 将最终的包复制到远程的仓库，以让其它开发人员与项目共享。</div></pre></td></tr></table></figure></p>
<p>运行任何一个阶段的时候，它前面的所有阶段都会被运行，这也是为什么我们运行mvn install的时候，代码会被编译，测试，打包的原因。此外，Maven的插件机制也是完全依赖Maven的生命周期的。</p>
<h4 id="3-site：生成项目站点"><a href="#3-site：生成项目站点" class="headerlink" title="3 site：生成项目站点"></a>3 site：生成项目站点</h4><p>这里经常用到的是site阶段和site-deploy阶段，用以生成和发布Maven站点。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">pre-site 执行一些需要在生成站点文档之前完成的工作 </div><div class="line">site 生成项目的站点文档 </div><div class="line">post-site 执行一些需要在生成站点文档之后完成的工作，并且为部署做准备 </div><div class="line">site-deploy 将生成的站点文档部署到特定的服务器上</div></pre></td></tr></table></figure></p>
<h3 id="Maven插件"><a href="#Maven插件" class="headerlink" title="Maven插件"></a>Maven插件</h3><p>Maven的核心仅仅定义了抽象的生命周期，具体的任务都是交由插件完成的。每个插件都能实现一个功能，每个功能就是一个插件目标。Maven的生命周期与插件目标相互绑定，以完成某个具体的构建任务。</p>
<h4 id="1-Maven编译插件"><a href="#1-Maven编译插件" class="headerlink" title="1 Maven编译插件"></a>1 Maven编译插件</h4><p>如果你足够细心，你会发现Maven工程默认jre是1.5的，我们可以安装插件指定编译用的jre版本，修改配置后，在工程上右键选择maven→update project configration，或使用alt+f5进行更新：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">build</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 编译插件，指定编译用的的jdk版本 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-compiler-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">source</span>&gt;</span>1.7<span class="tag">&lt;/<span class="name">source</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">target</span>&gt;</span>1.7<span class="tag">&lt;/<span class="name">target</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">encoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">encoding</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">build</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="2-安装Tomcat插件"><a href="#2-安装Tomcat插件" class="headerlink" title="2 安装Tomcat插件"></a>2 安装Tomcat插件</h4><p>使用maven创建一个web工程，不使用骨架，需要将打包方式改为war，然后点击finish，在工程中添加src/main/webapp/WEB-INF/web.xml配置信息，并在webapp下创建index.jsp文件：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">web-app</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></div><div class="line"><span class="tag">	<span class="attr">xmlns</span>=<span class="string">"http://java.sun.com/xml/ns/javaee"</span> <span class="attr">xmlns:web</span>=<span class="string">"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span></span></div><div class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span></span></div><div class="line"><span class="tag">	<span class="attr">id</span>=<span class="string">"WebApp_ID"</span> <span class="attr">version</span>=<span class="string">"2.5"</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">welcome-file-list</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>index.html<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>index.htm<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>index.jsp<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>default.html<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>default.htm<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">welcome-file</span>&gt;</span>default.jsp<span class="tag">&lt;/<span class="name">welcome-file</span>&gt;</span></div><div class="line">	<span class="tag">&lt;/<span class="name">welcome-file-list</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">web-app</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>运行tomcat插件目录是tomcat:run，默认运行的是tomcat6，比较老，我们可以添加tomcat7插件，再运行tomcat7:run<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.tomcat.maven<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>tomcat7-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">port</span>&gt;</span>80<span class="tag">&lt;/<span class="name">port</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">path</span>&gt;</span>/<span class="tag">&lt;/<span class="name">path</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><img src="http://om6odecqr.bkt.clouddn.com/MavenWeb%E5%BA%94%E7%94%A81.png" alt="MavenWeb应用1"></p>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p>继承时为了消除重复，可以把很多相同的配置提取出来。例如：grouptId，version等。</p>
<h4 id="1-创建父工程"><a href="#1-创建父工程" class="headerlink" title="1 创建父工程"></a>1 创建父工程</h4><p>父工程必须为pom工程：<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E7%88%B6%E5%B7%A5%E7%A8%8B.png" alt="Maven父工程"></p>
<h4 id="2-创建子工程"><a href="#2-创建子工程" class="headerlink" title="2 创建子工程"></a>2 创建子工程</h4><p>子工程必须设置父工程的GAV：<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E5%AD%90%E5%B7%A5%E7%A8%8B.png" alt="Maven子工程"><br>若要将现有工程继承父工程只需要在pom文件中添加parent节点即可：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">parent</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven_parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">parent</span>&gt;</span></div><div class="line">  </div><div class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>cn.sqdyy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven_son<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="3-父工程统一依赖jar包"><a href="#3-父工程统一依赖jar包" class="headerlink" title="3 父工程统一依赖jar包"></a>3 父工程统一依赖jar包</h4><p>在父工程中对jar包进行依赖，在子工程中都会继承此依赖。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="4-父工程统一管理版本号"><a href="#4-父工程统一管理版本号" class="headerlink" title="4 父工程统一管理版本号"></a>4 父工程统一管理版本号</h4><p>Maven使用dependencyManagement管理依赖的版本好，此处只是定义依赖jar包的版本号，并不实际依赖。如果子工程中需要依赖jar包还需要添加dependency节点。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 只是定义依赖的GAV，没有实际进行依赖，子工程如果需要依赖的话，需要在子工程中定义dependency --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">	  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">	   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">	   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">	   <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">	  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>子工程：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 子工程使用父类定义的依赖，不需要指定版本号 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="5-父工程中版本号提取"><a href="#5-父工程中版本号提取" class="headerlink" title="5 父工程中版本号提取"></a>5 父工程中版本号提取</h4><p>当父工程中定义的jar包越来越多，找起来越来越麻烦时，可以把版本号提取成一个属性集中管理：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">log4j.version</span>&gt;</span>1.2.9<span class="tag">&lt;/<span class="name">log4j.version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 只是定义依赖的GAV，没有实际进行依赖，子工程如果需要依赖的话，需要在子工程中定义dependency --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">	  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">	   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">	   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">	   <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;log4j.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">	  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="聚合"><a href="#聚合" class="headerlink" title="聚合"></a>聚合</h3><p>聚合一般是一个工程拆分成多个模块开发，每个模块是一个独立的工程，但是要是运行时必须把所有模块聚合到一起才是一个完整的工程，此时可以使用maven的聚合工程。</p>
<p>例如电商项目中，包括商品模块，订单模块，用户模块等，就可以对不同的模块单独创建工程，最终在打包时，将不同的模块聚合到一起。</p>
<p>一个项目中的表现层，业务层，持久层也可以分层创建不同的工程，最后打包运行时，再聚合到一起。</p>
<h4 id="1-创建聚合工程"><a href="#1-创建聚合工程" class="headerlink" title="1 创建聚合工程"></a>1 创建聚合工程</h4><p>聚合工程的打包方式必须时pom，一般聚合工程和父工程合并为一个工程，当然聚合工程也可以继承一个父工程，<strong>注意</strong>我在这块继承了parent，所以在执行tomcat7:run前需要将parent本地仓库中。<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E8%81%9A%E5%90%88%E5%B7%A5%E7%A8%8B.png" alt="Maven聚合工程"></p>
<h4 id="2-创建持久层工程"><a href="#2-创建持久层工程" class="headerlink" title="2 创建持久层工程"></a>2 创建持久层工程</h4><p>在maven-web工程上，点击创建Maven Module工程：<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E8%81%9A%E5%90%88Dao.png" alt="Maven_dao"></p>
<h4 id="3-创建业务层工程"><a href="#3-创建业务层工程" class="headerlink" title="3 创建业务层工程"></a>3 创建业务层工程</h4><p>在maven-web工程上，点击创建Maven Module工程：<br><img src="http://om6odecqr.bkt.clouddn.com/maven_service.png" alt="Maven_service"></p>
<h4 id="4-创建表现层工程"><a href="#4-创建表现层工程" class="headerlink" title="4 创建表现层工程"></a>4 创建表现层工程</h4><p>在maven-web工程上，点击创建Maven Module工程，注意表现层需要将打包方式设置为war：<br><img src="http://om6odecqr.bkt.clouddn.com/Maven_controller.png" alt="Maven_controller"></p>
<p>在maven_controller中添加web.xml和index.jsp。</p>
<h4 id="5-运行maven-web聚合工程"><a href="#5-运行maven-web聚合工程" class="headerlink" title="5 运行maven-web聚合工程"></a>5 运行maven-web聚合工程</h4><p>在maven_web中配置插件：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">modules</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">module</span>&gt;</span>maven_dao<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">module</span>&gt;</span>maven_controller<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">module</span>&gt;</span>maven_service<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">modules</span>&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;<span class="name">build</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></div><div class="line"> <span class="comment">&lt;!-- tomcat7插件 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.tomcat.maven<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>tomcat7-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">port</span>&gt;</span>80<span class="tag">&lt;/<span class="name">port</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">path</span>&gt;</span>/<span class="tag">&lt;/<span class="name">path</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">build</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>运行Tomcat7:run<br><img src="http://om6odecqr.bkt.clouddn.com/Maven%E8%81%9A%E5%90%88%E6%88%90%E5%8A%9F.png" alt="maven聚合成功"></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T09:57:35.573Z" itemprop="dateUpdated">2017年12月5日 17:57</time>
</span><br>


        如要转载请注明出处：<a href="/2017/08/08/Maven_primary_01/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/项目管理/">项目管理</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/&title=《Maven快速入门》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/&title=《Maven快速入门》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Maven快速入门》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/10/Mybatis_primary_01/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Mybatis快速入门一(基本应用)</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/01/深入Web请求过程(读书)/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">深入Web请求过程(深入分析JavaWeb)</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/&title=《Maven快速入门》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/&title=《Maven快速入门》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Maven快速入门》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/08/08/Maven_primary_01/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
